package com.ysy.tz.core.utils;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

public class NetWorkUtils {
    private static String ip;
    private static String mac;
    private static String hostName;

    public NetWorkUtils() {
    }

    public static String getHostName() {
        if (hostName == null) {
            try {
                hostName = InetAddress.getLocalHost().getHostName();
            } catch (UnknownHostException var1) {
                var1.printStackTrace();
            }
        }

        return hostName;
    }

    public static String getIp() {
        if (ip == null) {
            try {
                ip = InetAddress.getLocalHost().getHostAddress();
            } catch (UnknownHostException var1) {
                var1.printStackTrace();
            }
        }

        return ip;
    }

    public static String getMac() {
        if (NetWorkUtils.mac == null) {
            InetAddress ia = null;

            try {
                ia = InetAddress.getLocalHost();
            } catch (UnknownHostException var7) {
                var7.printStackTrace();
            }

            byte[] mac = new byte[0];

            try {
                mac = NetworkInterface.getByInetAddress(ia).getHardwareAddress();
            } catch (SocketException var6) {
                var6.printStackTrace();
            }

            StringBuffer sb = new StringBuffer("");

            for(int i = 0; i < mac.length; ++i) {
                if (i != 0) {
                    sb.append("-");
                }

                int temp = mac[i] & 255;
                String str = Integer.toHexString(temp);
                if (str.length() == 1) {
                    sb.append("0" + str);
                } else {
                    sb.append(str);
                }
            }

            NetWorkUtils.mac = sb.toString().toUpperCase();
        }

        return NetWorkUtils.mac;
    }

    public static List<String> getAllLocalMac() {
        Set<String> macs = new HashSet();

        try {
            Enumeration<NetworkInterface> enumeration = NetworkInterface.getNetworkInterfaces();

            label45:
            while(true) {
                StringBuffer stringBuffer;
                byte[] bytes;
                do {
                    NetworkInterface networkInterface;
                    do {
                        if (!enumeration.hasMoreElements()) {
                            break label45;
                        }

                        stringBuffer = new StringBuffer();
                        networkInterface = (NetworkInterface)enumeration.nextElement();
                    } while(networkInterface == null);

                    bytes = networkInterface.getHardwareAddress();
                } while(bytes == null);

                for(int i = 0; i < bytes.length; ++i) {
                    if (i != 0) {
                        stringBuffer.append("-");
                    }

                    int tmp = bytes[i] & 255;
                    String str = Integer.toHexString(tmp);
                    if (str.length() == 1) {
                        stringBuffer.append("0" + str);
                    } else {
                        stringBuffer.append(str);
                    }
                }

                String mac = stringBuffer.toString();
                macs.add(mac);
            }
        } catch (Exception var8) {
            var8.printStackTrace();
        }

        List<String> macList = new ArrayList(macs);
        return macList;
    }
}
